package pool;

import redis.clients.jedis.*;

import java.util.HashSet;
import java.util.Set;

/**
 * 连接池
 *
 * @author: MI
 * @email: 448341911@qq.com
 * @createTime: 2025/8/28 10:39
 * @updateUser: MI
 * @updateTime: 2025/8/28 10:39
 * @updateRemark: 修改内容
 * @version: 1.0
 */
public class JedisPoolTest {

    public static void main(String[] args) {
        ordinaryPool();
        shardedPool();

        JedisCluster();
//        sentinelPool();
    }

    /**
     * 普通连接池
     */
    public static void ordinaryPool() {
        JedisPool pool = new JedisPool(new JedisPoolConfig(),
                "192.168.110.36", // IP
                6379,             // 端口
                10000,            // 超时时间（ms）
                "seata@user123"); // 密码


//        JedisPool pool = new JedisPool("192.168.110.36", 6379);
        Jedis jedis = pool.getResource();
//        jedis.auth("seata@user123");
        jedis.set("qingshan", "平平无奇盆鱼宴");
        System.out.println(jedis.get("qingshan"));
    }

    /**
     * 分片连接池
     */
    public static void shardedPool() {
        // Redis服务器
        HostAndPort hp4 = new HostAndPort("192.168.110.36", 6379);

        // 连接池
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(50);
        poolConfig.setMaxIdle(10);
        poolConfig.setMinIdle(2);

        try (JedisPool jedisPool = new JedisPool(poolConfig, "192.168.110.36",
                6379,
                10000,              // connection timeout ms
//                10000,              // socket timeout ms
                "seata@user123")) {
            try (Jedis jedis = jedisPool.getResource()) {
                jedis.set("qingshan", "分片测试(单节点)");
                System.out.println(jedis.get("qingshan"));
            }
        }
    }


    /**
     * 分片连接池(Redis Cluster 集群 (推荐))
     */
    public static void JedisCluster() {
        // 3. 配置客户端参数（包含密码认证）
        JedisClientConfig clientConfig = DefaultJedisClientConfig.builder()
                .password("seata@user123") // 设置Redis密码
                .connectionTimeoutMillis(2000) // 连接超时时间（毫秒）
                .socketTimeoutMillis(2000)    // 读写超时时间（毫秒）
                .build();

        // 集群节点
        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort("192.168.110.66", 6380));
        nodes.add(new HostAndPort("192.168.110.66", 6379));
        nodes.add(new HostAndPort("192.168.110.67", 6379));

        try (JedisCluster jedisCluster = new JedisCluster(nodes, clientConfig)) {
            jedisCluster.set("qingshan", "分片测试(Cluster)");
            System.out.println(jedisCluster.get("qingshan"));
        }

    }

    /**
     * 哨兵连接池
     */
    public static void sentinelPool() {
        String masterName = "redis-master";
        Set<String> sentinels = new HashSet<String>();
        sentinels.add("192.168.44.186:26379");
        sentinels.add("192.168.44.187:26379");
        sentinels.add("192.168.44.188:26379");

        JedisSentinelPool pool = new JedisSentinelPool(masterName, sentinels);
        pool.getResource().set("qingshan", "哨兵" + System.currentTimeMillis() + "盆鱼宴");
        System.out.println(pool.getResource().get("qingshan"));
    }


}
